// pages/my/all-orders/all-orders.jsc
const request = require('../../../../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {


        //选项卡
        type: 2,
        typeIndex: 0,
        typeList: [{
            name: '配送',
            type: 2
        },
        {
            name: '快递',
            type: 1
        }
        ],
        page: 1,
        limit: 10,
        navlist: [{
            name: '全部',
            status: 'all',
            index: 0
        },
        {
            name: '待支付',
            status: 'payment',
            index: 1
        },
        {
            name: '待出库',
            status: 'chuku',
            index: 2
        },
        {
            name: '待配送',
            status: 'undelivered',
            index: 3
        },
        {
            name: '待收货',
            status: 'confirm',
            index: 4
        },
        // {
        //     name:'退款/售后',
        //     status:'aftersaleed',
        //     index:5
        // }

        ],
        //子导航选项卡-配送
        currentTab: 0,
        status: 'all', //订单状态
        list: [],
        isShowReason: false,
        explain: '',
        selectOrder: {},
        isShowChuku: false,
        isShowConfirm: false,
        pickup_adr: {},
        reason:{},
        timeInfo:{}
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.status && options.status != 'undefined') {
            this.setData({
                currentTab: this.data.navlist.find(el => el.status == options.status).index,
                status: options.status
            })
        } else {
            this.setData({
                currentTab: 0,
                status: 'all'
            })
        }
    },
    onShow() {
      if(getApp().globalData.isCenter){
        this.setData({
            list: [],
            page: 1
        })
        getApp().globalData.isCenter = false
        this.getList()
        this.getTypes()
      }
    },
    getTypes(){
      let _this = this
      request._post('/api?method=order.get_shouhou_types',{},'noload').then(res=>{
        _this.setData({
          pickup_adr: res.data.pickup_adr,
          reason: res.data.reason,
          timeInfo: res.data.pickup_time
        })
      })  
    },
    /**
     * 选项卡
     */
    typeClick(e) {
        const index = e.currentTarget.dataset.index;
        const item = e.currentTarget.dataset.item;
        this.setData({
            typeIndex: index,
            type: item.type,
            page: 1,
            isLastPage: false,
            list: []
        })
        this.getList()
    },
    /**
     * 支付收发货状态
     */
    tabClick(e) {
        const index = e.currentTarget.dataset.index;
        let item = e.currentTarget.dataset.item;
        this.setData({
            currentTab: index,
            status: item.status,
            page: 1,
            list: [],
            isLastPage: false,
        })
        this.getList()
    },
    // 详情
    onDetails(e) {
        let id = e.currentTarget.dataset.id
        let item = e.currentTarget.dataset.item
        if (item.order_type == 1) {
            //普通订单
            wx.navigateTo({
                url: '/pages/mine/pages/my/order-details/order-details?id=' + id,
            })
        } else {
            // 拼团订单
            wx.navigateTo({
                url: '/pages/home/pages/my/group-details/group-details?id=' + id,
            })
        }
    },
    getList() {
        let _this = this
        request._post('/api?method=order.getorderlist', {
            status: this.data.status,
            page: this.data.page,
            limit: this.data.limit,
            // type: this.data.type
        }).then(res => {
          if(_this.data.page == 1){
            _this.setData({
              list:[]
            })
          }
          if (res.data.list.length == 0 || res.data.list == null) {
              _this.setData({
                  isLastPage: true,
              })
          }
          _this.setData({
              list: _this.data.list.concat(res.data.list)
          })
        })
    },
    // 上拉加载
    onReachBottom: function () {
        if (this.data.isLastPage) {
            return
        }
        let page = this.data.page
        page++
        this.setData({
            page,
        })
        this.getList()
    },
    // 待出库退款
    handleAfterOrder(e){
      // let type = e.currentTarget.dataset.type
      let item = e.currentTarget.dataset.item
      this.setData({
        selectOrder: item,
    })
      this.setData({
        isShowChuku: !this.data.isShowChuku
      })
    },
    // 操作订单
    handleOrder(e) {
        let type = e.currentTarget.dataset.type
        let item = e.currentTarget.dataset.item
        this.setData({
            selectOrder: item,
        })
        console.log(this.data.selectOrder)
        if (type == 1) { //取消订单
            this.setData({
                isShowReason: true,
            })
        } else if (type == 2) { //删除订单
            this.delOrderApi()
        } else if (type == 3) { //提醒发货
            this.remindSend()
        } else if (type == 4) { //延长收货
            this.lengthenOrder()
        } else if (type == 5) { //再次购买
            this.reBuy()
        } else if (type == 6) { // 申请售后
          this.setData({
            isShowConfirm: !this.data.isShowConfirm
          })
        }else  if (type == 7) { //确认收货 
          this.sureGetOrder()
        }else  if (type == -1) { //确认收货 
          this.payApi()
        }
    },
    closeChuku(){
      this.setData({
        isShowChuku: !this.data.isShowChuku
      })
    },
    //待待配送售后提交
    submitConfirm(e){
      console.log(e)
      let data = e.detail
      let _this = this
      let selectOrder = this.data.selectOrder
      data.order_id = selectOrder.order_id
      request._get('/api?method=order.addaftersales', data).then(res => {
        if(_this.data.status == 'confirm'){
          getApp().toast('申请已提交，待取件时审核')
        }else{
          getApp().toast('提交成功')
        }
          _this.setData({
              page: 1,
              isLastPage: false,
              isShowChuku: false,
              isShowConfirm: false
          })
          setTimeout(function () {
              _this.getList()
          }, 1000)
      })
    },
    closeConfirm(){
      this.setData({
        isShowConfirm: !this.data.isShowConfirm
      })
    },
    // 关闭弹框
    closeReason() {
        this.setData({
            isShowReason: false
        })
    },
    // 支付接口api
    payApi() {
      // 支付接口
      let _this = this
      let order_id = _this.data.selectOrder.order_id
      request._post('/api?method=user.pay', {
          ids: order_id,
          payment_code:'wechatpay',
          payment_type: 1
      }).then(res => {
        let obj = res.data
        wx.requestPayment({
            timeStamp: obj.timeStamp,
            nonceStr: obj.nonceStr,
            package: obj.package,
            signType: obj.signType,
            paySign: obj.paySign,
            signType: obj.signType,
            success(res) {
                getApp().toast('支付成功')
                getApp().sendMessage(order_id)
                _this.setData({
                  page: 1,
                  isLastPage: false,
                })
                setTimeout(function () {
                    _this.getList()
                }, 1000)
            },
            fail(res) {
                getApp().toast('支付失败，请重新支付')
            }
        })
      }).catch(function (res) {
          // 报错支付失败
      })
    },
    // 提交取消订单
    submit(e) {
        this.setData({
            explain: e.detail,
            isShowReason: false
        })
        this.cancelOrderApi()
    },
    // 取消订单api
    cancelOrderApi() {
        let _this = this
        let selectOrder = this.data.selectOrder
        request._get('/api?method=order.cancel', {
            order_id: selectOrder.order_id,
            explain: this.data.explain
        }).then(res => {
            getApp().toast(res.msg)
            _this.setData({
                page: 1,
                isLastPage: false,
            })
            setTimeout(function () {
                _this.getList()
            }, 1000)
        })
    },
    // 删除订单
    delOrderApi() {
        let _this = this
        wx.showModal({
            title: '提示',
            content: '确认删除当前订单？',
            success(res) {
                if (res.confirm) {
                    request._get('/api?method=order.del', {
                        order_id: _this.data.selectOrder.order_id
                    }).then(res => {
                        getApp().toast(res.msg)
                        _this.setData({
                            isLastPage: false,
                            page: 1
                        })
                        setTimeout(function () {
                            _this.getList()
                        }, 1000)
                    })
                } else if (res.cancel) { }
            }
        })
    },
    // 申请售后
    applyShouhou() {
      let _this = this
      wx.showModal({
          title: '提示',
          content: '是否申请售后？',
          success(res) {
              if (res.confirm) {
                  request._post('/api?method=order.addAftersale', {
                      order_id: _this.data.selectOrder.order_id
                  }).then(res => {
                      getApp().toast(res.msg)
                      _this.setData({
                          isLastPage: false,
                          page: 1
                      })
                      setTimeout(function () {
                          _this.getList()
                      }, 1000)
                  })
              } else if (res.cancel) { }
          }
      })
    },
    // 确认收货
    sureGetOrder() {
      let _this = this
      request._post('/api?method=order.confirm', {
          order_id: _this.data.selectOrder.order_id
      }).then(res => {
        console.log(res)
        if(res.extraData.transaction_id){
          let isResult = getApp().sureGetOrder(res.extraData)
          if(isResult){
            _this.setData({
              isLastPage: false,
              page: 1
            })
            setTimeout(function () {
                _this.getList()
            }, 1000)
          }
        }else{
          getApp().toast('收货成功,可去全部订单里，查看！')
          _this.setData({
            isLastPage: false,
            page: 1
          })
          setTimeout(function () {
              _this.getList()
          }, 1000)
        }
      })
        // wx.showModal({
        //     title: '提示',
        //     content: '是否确认收货？',
        //     success(res) {
        //         if (res.confirm) {
            
        //         } else if (res.cancel) { }
        //     }
        // })
    },
    // 提醒发货
    remindSend() {
        let _this = this
        request._post('/api?method=order.orderRemind', {
            order_id: this.data.selectOrder.order_id
        }).then(res => {
            getApp().toast(res.msg)
            setTimeout(function () {
                _this.setData({
                    page: 1,
                    isLastPage: false
                })
                _this.getList()
            }, 1000)
        })
    },
    // 延长收货
    lengthenOrder() {
        let _this = this
        wx.showModal({
            title: '提示',
            content: '确认延长收货？',
            success(res) {
                if (res.confirm) {
                    request._post('/api?method=order.orderCargo', {
                        order_id: _this.data.selectOrder.order_id
                    }).then(res => {
                        getApp().toast(res.msg)
                    })
                } else if (res.cancel) { }
            }
        })
    },
    // 再次购买
    reBuy() {
        let orderInfo = this.data.selectOrder
        request._post('/api?method=order.buyagain', {
            order_id: orderInfo.order_id
        }, 'noload').then(res => {
            getApp().globalData.rebuyIds = res.data
            setTimeout(function () {
                wx.switchTab({
                    url: '/pages/shoppingCart/shoppingCart'
                })
            }, 1000)
        })
    },

})